﻿using System.Linq;
using Microsoft.FxCop.Sdk;

namespace LINQtoFxCop.Metrics
{
  public static class MethodMetrics
  {
    public static int NbILInstructions(this Method method)
    {
      return
        method.Instructions
          .Where(instruction => !instruction.OpCode.ToString().StartsWith("_"))
          .Count();
    }
    public static int NbParameters(this Method method)
    {
      if (method.Parameters == null)
        return 0;
      return method.Parameters.Count;
    }
    public static int NbVariables(this Method method)
    {
      if (method.Locals == null)
        return 0;
      return method.Locals.Count;
    }
    public static int NbOverloads(this Method method)
    {
      return
        method.DeclaringType.Members.OfType<Method>()
          .Where(overload => overload.Name.Name.Equals(method.Name.Name))
          .Count();
    }
  }
}
